#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""应用配置管理"""

import os
from typing import Any, Dict, Optional
from pydantic import  validator
from pydantic_settings import BaseSettings
from dotenv import load_dotenv

# 加载.env文件中的环境变量
load_dotenv()

class Settings(BaseSettings):
    """应用配置类"""
    # 基本配置
    DEBUG: bool = os.getenv("DEBUG", "false").lower() == "true"
    API_PREFIX: str = os.getenv("API_PREFIX", "/api")
    
    # 服务器配置
    HOST: str = os.getenv("HOST", "0.0.0.0")
    PORT: int = int(os.getenv("PORT", "8080"))
    
    # 上传文件配置
    MAX_UPLOAD_SIZE: int = int(os.getenv("MAX_UPLOAD_SIZE", "100"))  # MB
    UPLOAD_DIR: str = "uploads"
    
    # PDF处理配置
    EXTRACT_TABLES_ONLY: bool = os.getenv("EXTRACT_TABLES_ONLY", "true").lower() == "true"
    DETECT_TABLE_TITLES: bool = os.getenv("DETECT_TABLE_TITLES", "true").lower() == "true"
    MERGE_CONTINUED_TABLES: bool = os.getenv("MERGE_CONTINUED_TABLES", "true").lower() == "true"
    
    # 日志配置
    LOG_LEVEL: str = os.getenv("LOG_LEVEL", "INFO")
    LOG_FILE: str = os.getenv("LOG_FILE", "logs/app.log")
    
    @validator("MAX_UPLOAD_SIZE")
    def validate_max_upload_size(cls, v: int) -> int:
        """验证最大上传大小"""
        if v <= 0:
            return 10  # 默认10MB
        return v
    
    class Config:
        case_sensitive = True

# 创建全局设置对象
settings = Settings()